package com.fuqi.android.zhongcai.base.view.activity;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.api.ProductIntroApi;
import com.fuqi.android.zhongcai.base.request.result.ProductIntroResult;
import com.fuqi.android.zhongcai.base.view.adapter.BaseRecyclerViewAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.GongYiAdapter;
import com.fuqi.android.zhongcai.base.view.widget.CustomProgress;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.LogUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import butterknife.Bind;

public class GongYiActivity extends BaseActivity {


    @Bind(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private ArrayList<ProductIntroResult> listData;
    private GongYiAdapter gongYiAdapter;


    @Override
    public void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);

        listData = new ArrayList<ProductIntroResult>();
        gongYiAdapter = new GongYiAdapter(GongYiActivity.this,listData);
        mRecyclerView.setAdapter(gongYiAdapter);
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(
//                this, DividerItemDecoration.HORIZONTAL_LIST));
    }

    @Override
    public void initListener() {
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage=1;
                CustomProgress.show(GongYiActivity.this, "正在处理...",true,null);
                RequestManager.getInstance().call(new ProductIntroApi(new ProductIntroApi.ProductIntroParams(""),
                        respGongYiListener, errorListener));
            }

            @Override
            public void onLoadMore() {
                currentPage ++;
                CustomProgress.show(GongYiActivity.this, "正在处理...",true,null);
                RequestManager.getInstance().call(new ProductIntroApi(new ProductIntroApi.ProductIntroParams(""),
                        respGongYiListener, errorListener));

            }
        });

        gongYiAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<ProductIntroResult>() {
            @Override
            public void onItemClick(View view, int position, ProductIntroResult data) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",data);
                ActivityUtils.startActivity(GongYiActivity.this,GongYiByTypeActivity.class,bundle);

            }
        });

    }

    @Override
    public void initData() {

        titleBar.setTitle("产品工艺");

        RequestManager.getInstance().call(new ProductIntroApi(new ProductIntroApi.ProductIntroParams(""), respGongYiListener, errorListener));

    }


    Response.Listener<ProductIntroResult[]> respGongYiListener = new Response.Listener<ProductIntroResult[]>() {

        @Override
        public void onResponse(ProductIntroResult[] response) {
            CustomProgress.close();
            if (HandleCode.requestSuccess()) {
                if (response.length == 0)
                    mRecyclerView.noMoreLoading();
                if (currentPage == 1 && listData != null)
                    listData.clear();
                for (int i = 0; i < response.length; i++) {
                    listData.add(response[i]);
                }

                gongYiAdapter.notifyDataSetChanged();
            }
            LogUtils.i("response OrderListResult: " + GsonHelper.writeValue(response));
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            CustomProgress.close();
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };


    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_gong_yi;
    }
}
